package com.company.jncz.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.company.jncz.entity.Topic;
import com.company.jncz.service.TopicService;

public class TopicServiceImpl implements TopicService {
	private static final List<Topic> datas = new ArrayList<Topic>();
	static{
		Topic topic1 = new Topic();
		topic1.setId(""+System.currentTimeMillis());
		topic1.setTitle("The first topic");
		topic1.setContent("The content of first topic");
		Topic topic2 = new Topic();
		topic2.setId(""+System.currentTimeMillis());
		topic2.setTitle("The second topic");
		topic2.setContent("The content of second topic");
		datas.add(topic1);
		datas.add(topic2);
	}
	@Override
	public List<Topic> getAllTopic() {
		return datas;
	}

	@Override
	public void saveTopic(Topic topic) {
		topic.setId(""+System.currentTimeMillis());
		datas.add(topic);
	}

	@Override
	public void delete(String id) {
		int topicIndex = -1;
		for(int i=0;i<datas.size();i++){
			if(datas.get(i).getId().equals(id)){
				topicIndex = i;
				break;
			}
		}
		if(topicIndex != -1)
			datas.remove(topicIndex);
	}
	
	

}
